﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.EBS
{
    public static class Storage
    {
        private static List<NamespaceEntry> NamespaceList = new List<NamespaceEntry>();
        private static object s_lockObj = new object();

        /// <summary>
        /// 获取或创建Namespace
        /// </summary>
        /// <param name="namespace"></param>
        /// <returns></returns>
        public static NamespaceEntry GetOrCreate(string @namespace)
        {
            NamespaceEntry entry = null;
            for (int index = 0; index < NamespaceList.Count; index++)
            {
                if(NamespaceList[index].Name == @namespace)
                {
                    entry = NamespaceList[index];
                    break;
                }
            }

            if(entry == null)
            {
                lock(s_lockObj)
                {
                    for (int index = 0; index < NamespaceList.Count; index++)
                    {
                        if (NamespaceList[index].Name == @namespace)
                        {
                            entry = NamespaceList[index];
                            break;
                        }
                    }

                    if(entry==null)
                    {
                        entry = new NamespaceEntry();
                        entry.Name = @namespace;
                        NamespaceList.Add(entry);
                        return entry;
                    }
                }
            }
            return entry;
        }

        public static NamespaceEntry Get(string @namespace)
        {
            NamespaceEntry entry = null;
            for (int index = 0; index < NamespaceList.Count; index++)
            {
                if (NamespaceList[index].Name == @namespace)
                {
                    entry = NamespaceList[index];
                    break;
                }
            }

            return entry;
        }

        public static void Remove(NamespaceEntry entry)
        {
            lock(s_lockObj)
            {
                for (int index = 0; index < NamespaceList.Count; index++)
                {
                    if (NamespaceList[index].Name == entry.Name)
                    {
                        NamespaceList.RemoveAt(index);
                        break;
                    }
                }
            }
        }
    }
}
